package com.adopapa.nezhacloud.authc.controller;

import java.time.Instant;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.adopapa.nezhacloud.authc.domain.DepartmentInfo;
import com.adopapa.nezhacloud.authc.service.DepartmentInfoService;
import com.adopapa.nezhacloud.common.RestResponse;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.codegen.SqlGen;
import com.adopapa.nezhacloud.ddd.conf.AppConstants;
import com.adopapa.nezhacloud.ddd.controller.BaseController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "组织管理")
@RestController
@RequestMapping("/authc/departmentinfo")
public class DepartmentInfoController extends BaseController {

	@Autowired
	private DepartmentInfoService departmentInfoService;

	@Operation(summary = "分页查询")
	@Parameters({
		@Parameter(name = "pageSize", description = "每页记录数", required = false, in = ParameterIn.QUERY),
		@Parameter(name = "deptName", description = "部门名称", required = false, in = ParameterIn.QUERY),
	})
	@SqlGen(name = { "权限管理", "组织管理", "分页查询" }, method = SqlGen.METHOD_GET, path = "/departmentinfos/{pageNo}")
	@GetMapping(path = "/departmentinfos/{pageNo}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getDepartmentInfoPage(@RequestHeader String authorization, //
			@PathVariable Integer pageNo, //
			@RequestParam(required = false) String deptName, //
			@RequestParam(required = false) Integer pageSize) {

		DepartmentInfo departmentInfo = new DepartmentInfo();

		departmentInfo.setDeptName(deptName);

		pageSize = Tools.empty(pageSize) ? AppConstants.DEFAULT_PAGE_SIZE : pageSize;

		Page<DepartmentInfo> page = departmentInfoService.getDepartmentInfoPage(pageNo, pageSize, departmentInfo);

		return RestResponse.ok(page);
	}

	@Operation(summary = "单个查询")
	@SqlGen(name = { "权限管理", "组织管理", "单个查询" }, method = SqlGen.METHOD_GET, path = "/departmentinfo/{id}")
	@GetMapping(path = "/departmentinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getHappyDemo(@RequestHeader String authorization, //
			@PathVariable String id) {
		DepartmentInfo departmentInfo = departmentInfoService.getDepartmentInfoById(id);
		return RestResponse.ok(departmentInfo);
	}

	@Operation(summary = "编辑组织")
	@io.swagger.v3.oas.annotations.parameters.RequestBody(content = {
			@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
					@ExampleObject("{\r\n"
				+ "    \"deptName\": \"组织11\",\r\n"
				+ "    \"upperId\": \"40289fe48ad9737c018ad973cb320000\",\r\n"
				+ "    \"remark\": \"组织1组织1组织1组织1组织1222222\"\r\n"
				+ "}")
			} )
	})
	@SqlGen(name = { "权限管理", "组织管理", "编辑组织" }, method = SqlGen.METHOD_POST, path = "/departmentinfos")
	@PostMapping(path = "/departmentinfos", produces = MediaType.APPLICATION_JSON_VALUE, //
			consumes = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse saveDepartmentInfo(@RequestHeader String authorization, //
			@RequestBody Map<String, Object> params) {

		String id = getMapValue(params, "id", String.class);

		String upperId = getMapValue(params, "upperId", String.class);
		String deptName = getMapValue(params, "deptName", String.class);
		String remark = getMapValue(params, "remark", String.class);

		DepartmentInfo departmentInfo = null;
		if (Tools.empty(id)) {
			departmentInfo = new DepartmentInfo();
			departmentInfo.setCreateTime(Instant.now());
		} else {
			departmentInfo = departmentInfoService.getDepartmentInfoById(id);
			if (Tools.empty(departmentInfo)) {
				throw new RuntimeException("指定id对应信息不存在");
			}
		}

		DepartmentInfo upper = null;
		if (!Tools.empty(upperId)) {
			upper = departmentInfoService.getDepartmentInfoById(upperId);
			if (Tools.empty(upper)) {
				throw new RuntimeException("指定upperId对应信息不存在");
			}
			departmentInfo.setUpper(upper);
		}

		departmentInfo.setDeptName(deptName);
		departmentInfo.setRemark(remark);

		departmentInfoService.saveDepartmentInfo(departmentInfo);

		return RestResponse.ok();

	}

	@Operation(summary = "删除组织")
	@SqlGen(name = { "权限管理", "组织管理", "删除组织" }, method = SqlGen.METHOD_DELETE, path = "/departmentinfo/{id}")
	@DeleteMapping(path = "/departmentinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse deleteDepartmentInfo(@RequestHeader String authorization, //
			@PathVariable String id) {
		try {
			departmentInfoService.deleteDepartmentInfo(id);
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}

		return RestResponse.ok();
	}

}
